package com.zhiwang.xiaoqu.ui.withdraw

import android.graphics.drawable.LevelListDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.zhiwang.xiaoqu.R
import com.zhiwang.xiaoqu.mvp.model.response.WithDrawItem
import me.drakeet.multitype.ItemViewBinder

class WithDrawItemViewBinder : ItemViewBinder<WithDrawItem, WithDrawItemViewBinder.WithDrawItemViewHolder>() {

    var prev: Int = 0
    var curr: Int = 0


    val payLoad = listOf(Object())

    var onItemClickListener = View.OnClickListener { v ->
        val position = (v.layoutParams as RecyclerView.LayoutParams).viewAdapterPosition
        prev = curr
        curr = position

        if (curr != prev) {
            adapter.notifyItemChanged(curr, payLoad)
            adapter.notifyItemChanged(prev, payLoad)
        }

    }

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): WithDrawItemViewHolder {
        return WithDrawItemViewHolder(
            inflater.inflate(
                R.layout.withdraw_item,
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: WithDrawItemViewHolder, item: WithDrawItem) {

    }

    override fun onBindViewHolder(holder: WithDrawItemViewHolder, item: WithDrawItem, payloads: MutableList<Any>) {

        if (payloads.isEmpty()) {
            holder.textView.text = item.num.plus("元")
            Glide.with(holder.itemView.context)
                .load(item.tagUrl)
                .into(holder.imageView)
        }
        if (holder.adapterPosition == curr) {
            (holder.textView.background as LevelListDrawable).level = 1
            holder.textView.setTextColor(ContextCompat.getColor(holder.itemView.context, R.color.colorPrimary))
        } else {
            (holder.textView.background as LevelListDrawable).level = 0
            holder.textView.setTextColor(ContextCompat.getColor(holder.itemView.context, R.color.colorBlack_1))
        }

    }


    inner class WithDrawItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val textView: TextView = itemView.findViewById(R.id.withdraw_amount)

        val imageView: ImageView = itemView.findViewById(R.id.iv_tag)

        init {
           itemView.setOnClickListener(onItemClickListener)
        }
    }
}